package com.sp.business.normalization.impl.mf2;

import com.sp.business.objectmodel.NormalizedData;
import com.sp.generated.xml.firemon.NATRule;
import com.sp.generated.xml.firemon.NATRuleCollection;
import com.sp.generated.xml.firemon.Policy;
import com.sp.generated.xml.firemon.SecurityRule;
import com.sp.generated.xml.firemon.SecurityRuleCollection;

/**
 * ExpressionBuilder for Policies and Rules. For explanation see:
 * 
 * http://martinfowler.com/bliki/ExpressionBuilder.html
 * 
 * @author mtaylor
 */
public class PolicyBuilder {
    private Policy policy;

    private PolicyBuilder() {
        policy = new Policy();
        String name = "Policy";
        policy.setName(NormalizedData.createStringProperty(name));
        policy.setXsid(name);
        policy.setUID("");
        policy.setSecurityRules(new SecurityRuleCollection());
        policy.setNATRules(new NATRuleCollection());
    }

    public static PolicyBuilder beginGlobalPolicy() {
        return new PolicyBuilder();
    }

    public static PolicyBuilder beginPolicy(String fromZone, String toZone) {
        PolicyBuilder builder = new PolicyBuilder();
        String name = "From: " + fromZone + " To: " + toZone;
        return builder.name(name).xsid(name);
    }

    public Policy build() {
        // TODO validate Policy?
        return policy;
    }

    public PolicyBuilder name(String name) {
        policy.setName(NormalizedData.createStringProperty(name));
        return this;
    }

    public PolicyBuilder xsid(String xsid) {
        policy.setXsid(xsid);
        return this;
    }

    public PolicyBuilder addSecurityRule(SecurityRule securityRule) {
        policy.getSecurityRules().getSecurityRules().add(securityRule);
        return this;
    }

    public PolicyBuilder addNATRule(NATRule natRule) {
        policy.getNATRules().getNATRules().add(natRule);
        return this;
    }
}
